<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      const value = {
        arr: [1, 2, 3, [4, 5, { name: "jack" }]],
        msg: "this is a book",
        stu: {
          name: "marry",
          hobbies: ["a", "b", ["c", { msg: "ok" }]],
        },
      };

      value.q = value;

      function deepClone(value) {
        const cache = new WeakMap();
        function _deepClone(value) {
          if (value === null || typeof value !== "object") {
            return value;
          }

          if (cache.has(value)) {
            return cache.get(value);
          }

          let result = Array.isArray(value) ? [] : {};
          cache.set(value, result);

          for (let key in value) {
            if (value.hasOwnProperty(key)) {
              result[key] = _deepClone(value[key]);
            }
          }

          return result;
        }
        return _deepClone(value);
      }

      console.log(deepClone(value));
    </script>
  </body>
</html>
